<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\service\IndexService;
use app\api\service\CouponService;
use app\api\service\CooperateService;
use app\api\service\ServicePackageService;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 支付方式
     *
     */
    public function payment()
    {
        $res = IndexService::getPayment();
        return json($res);
    }

    /**
     * 引导页
     *
     */
    public function guide()
    {
        $res = IndexService::getGuidePage();
        return json($res);
    }

    /**
     * 轮播图
     *
     */
    public function banner()
    {
        $res = IndexService::getBanner();
        return json($res);
    }

    /**
     * 优惠卷列表
     *
     */
    public function coupon()
    {
        $data = $this->request->get();
        $res = CouponService::getCouponList($data);
        return json($res);
    }

    /**
     * 合作专区
     *
     */
    public function cooperate()
    {
        $res = CooperateService::getCooperate();
        return json($res);
    }

    /**
     * 服务列表
     *
     */
    public function service()
    {
        $data = $this->request->get();
        $res = ServicePackageService::getServicePackageList($data);
        return json($res);
    }

    /**
     * 联系客服
     *
     */
    public function customer()
    {
        $res = IndexService::getCustomer();
        return json($res);
    }

    /**
     * 获取协议
     *
     */
    public function protocol()
    {
        $data = $this->request->post();
        $res = IndexService::getProtocol($data);
        return json($res);
    }

    /**
     * 常见问题
     *
     */
    public function question()
    {
        $data = $this->request->post();
        $res = IndexService::getQuestion($data);
        return json($res);
    }

    public function test()
    {
        $batchSize = 99; // 每次批量更新的数量
        // 在营业的管家
        \app\admin\model\Steward::where(['status'=>'normal','service_switch'=>2,'service_status'=>1])
            ->chunk($batchSize, function($data) {
                $mobiles = '';
                foreach ($data as $row) {
                    // 进行发送短信操作
                    $mobile = \app\admin\model\User::where(['id'=>$row['user_id'],'status'=>'normal'])->value('mobile');
                    $mobiles .= $mobile . ',';
                }
//                dump($mobiles);
                sendSmsNotice($mobiles,0);
            });

        // 在营业时间段的管家
        $hour = date('H') . ':00';
        \app\admin\model\Steward::where(['status'=>'normal','service_switch'=>3,'service_status'=>1])->where(['start'=>['<',$hour]])->where(['over'=>['>',$hour]])
            ->chunk($batchSize, function($data) {
                $mobiles = '';
                foreach ($data as $row) {
                    // 进行发送短信操作
                    $mobile = \app\admin\model\User::where(['id'=>$row['user_id'],'status'=>'normal'])->value('mobile');
                    $mobiles .= $mobile . ',';
                }
//                dump($mobiles);
                sendSmsNotice($mobiles,0);
            });
    }


    public function test1()
    {
        $batchSize = 99; // 每次批量更新的数量
        // 示例：获取分类ID为1的所有子分类ID
        $categoryId = 1;
        $subCategoryIds = $this->getCategorySubIds($categoryId);
        print_r($subCategoryIds);

        // ... 其他代码 ...
    }

    /**
     * 获取指定分类及其所有子分类的ID
     *
     * @param int $categoryId 指定分类ID
     * @return array 子分类ID数组
     */
    public function getCategorySubIds($categoryId)
    {
        $categoryIds = [];
        $this->fetchSubCategoryIds($categoryId, $categoryIds);
        return $categoryIds;
    }

    /**
     * 递归获取子分类ID
     *
     * @param int $categoryId 当前分类ID
     * @param array &$categoryIds 存储子分类ID的数组
     */
    private function fetchSubCategoryIds($categoryId, &$categoryIds)
    {
        $categories = Category::where('pid', $categoryId)->select();
        foreach ($categories as $category) {
            $categoryIds[] = $category->id;
            $this->fetchSubCategoryIds($category->id, $categoryIds);
        }
    }

}
